/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Equipo.java
 *
 * Created on 19/09/2009, 14:22:37
 */

package tpgrandt.UI;

import tpgrandt.UI.model.JugadorModel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import tpgrandt.control.GestionEquipo;
import tpgrandt.excepciones.*;
import tpgrandt.varios.*;

/**
 *
 * @author nacho
 */
public class CreacionEquipoDeFantasia extends javax.swing.JInternalFrame implements MouseListener {

    private GestionEquipo gestion;
    private EquipoPrimera[] equipos;
    private JugadorModel jugadores1ra;
    private JugadorModel titulares;
    private JugadorModel suplentes;
    private int jugSeleccionado = -1;
    private JLabel[] indices;
    private Jugador jugadores[];
    private static final int jugXpag = 20;
    private static final int pagVisibles = 10;
    private int pagSeleccionada = 1;


    /** Creates new form Equipo */
    public CreacionEquipoDeFantasia(GestionEquipo gestion) {
        super("Crear equipo de fantasía", false, false, false, false);
        this.gestion = gestion;
        initComponents();
	pack();

	jFormattedTextField2.setText(gestion.ultimoPlantel().getCotizacion() + "");
	jFormattedTextField3.setText(PlantelFecha.cotizacion_max - gestion.ultimoPlantel().getCotizacion() + "");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(920, 540));

        Jugador tit[] = new Jugador[11];
        /*for (int i = 0; i < 11; i++)
        tit[i] = gestion.getEquipoFantasia().ultimoPlantel().getJugador(i + 1);*/
        titulares = new JugadorModel(tit);
        jTable1.setModel(titulares);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        TableColumn tc = jTable1.getColumn("Puesto");
        tc.setPreferredWidth(45);
        tc = jTable1.getColumn("Apellido y nombres");
        tc.setPreferredWidth(150);
        tc = jTable1.getColumn("Club");
        tc.setPreferredWidth(100);
        tc = jTable1.getColumn("Cotización");
        tc.setPreferredWidth(60);

        Jugador sup[] = new Jugador[4];
        /*for (int i = 11; i < 15; i++)
        sup[i - 11] = gestion.getEquipoFantasia().ultimoPlantel().getJugador(i + 1);*/
        suplentes = new JugadorModel(sup);
        jTable2.setModel(suplentes);
        jTable2.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);
        tc = jTable2.getColumn("Puesto");
        tc.setPreferredWidth(45);
        tc = jTable2.getColumn("Apellido y nombres");
        tc.setPreferredWidth(150);
        tc = jTable2.getColumn("Club");
        tc.setPreferredWidth(100);
        tc = jTable2.getColumn("Cotización");
        tc.setPreferredWidth(60);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Equipo de fantasía:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Titulares:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Suplentes:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Puesto:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Arquero", "Defensor", "Volante", "Delantero" }));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Club:");

        jComboBox2.setMaximumRowCount(30);
        equipos = gestion.listarEquipos();
        String e[] = new String[equipos.length + 1];
        e[0] = "Todos";
        for (int i = 1; i < equipos.length + 1; i++)
        e[i] = equipos[i - 1].getNombre();
        jComboBox2.setModel(new DefaultComboBoxModel(e));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Cotización:");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton1ActionPerformed(null);
        /*
        Jugador l[] = gestion.listarJugadores(0, 0, 0, 50000);
        jugadores1ra = new JugadorModel(l);*/
        jTable3.setModel(jugadores1ra);
        jTable3.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane3.setViewportView(jTable3);
        tc = jTable3.getColumn("Puesto");
        tc.setPreferredWidth(45);
        tc = jTable3.getColumn("Apellido y nombres");
        tc.setPreferredWidth(150);
        tc = jTable3.getColumn("Club");
        tc.setPreferredWidth(100);
        tc = jTable3.getColumn("Cotización");
        tc.setPreferredWidth(60);

        jButton2.setText("Agregar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Quitar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Dinero gastado:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Dinero disponible:");

        jButton4.setText("Guardar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Cancelar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "300", "500", "1000", "3000", "5000" }));

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MAXIMO", "5000", "3000", "1000", "500", "300" }));

        jLabel19.setText("*");

        jFormattedTextField2.setEditable(false);
        jFormattedTextField2.setText("< Datos pertinentes>");

        jFormattedTextField3.setEditable(false);
        jFormattedTextField3.setText("< Datos pertinentes>");

        jPanel1.setAlignmentX(jPanel1.RIGHT_ALIGNMENT);
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));
        jPanel1.add(jLabel11);
        jPanel1.add(jLabel12);
        jPanel1.add(jLabel13);

        jLabel9.setText("Paginas:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel8)))
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel19))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton5))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(231, 231, 231)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel9))
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        gestion.ultimoPlantel().cancelar();
        dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            if (jTable3.getSelectedRow() < 0)
                throw new JugadorNoSeleccionadoException();

            if(jugadores1ra.getValueAt(jTable3.getSelectedRow(), 4) instanceof String)
                throw new JugadorNoSeleccionadoException();

            int pos = gestion.ultimoPlantel().agregar(
                    (Jugador) jugadores1ra.getValueAt(jTable3.getSelectedRow(), 4));

            if (pos < 12)
                titulares.setValueAt(jugadores1ra.getValueAt(jTable3.getSelectedRow(), 4), pos - 1, 0);
            else
                suplentes.setValueAt(jugadores1ra.getValueAt(jTable3.getSelectedRow(), 4), pos - 12, 0);

            jTable1.updateUI();
            jTable2.updateUI();
            jFormattedTextField2.setText(gestion.ultimoPlantel().getCotizacion() + "");
            jFormattedTextField3.setText(PlantelFecha.cotizacion_max - gestion.ultimoPlantel().getCotizacion() + "");

        } catch(Excepcion e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), e.getTitulo(), e.getTipoMensaje());
        }
        jFormattedTextField2.updateUI();
        jFormattedTextField3.updateUI();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            if (jugSeleccionado < 0) {
                throw new JugadorNoSeleccionadoException();
            }
            JTable tabla;
            JugadorModel jm;
            int aux = 0;
            if (jugSeleccionado < 11) {
                tabla = jTable1;
                jm = titulares;
                aux = 1;
            } else {
                tabla = jTable2;
                jm = suplentes;
                aux = 12;
            }
            if (jm.getValueAt(tabla.getSelectedRow(), 4) != null) {
                gestion.ultimoPlantel().quitar(tabla.getSelectedRow() + aux);
                jm.setValueAt(null, tabla.getSelectedRow(), 0);
                tabla.updateUI();
            }
            jFormattedTextField2.setText(gestion.ultimoPlantel().getCotizacion() + "");
            jFormattedTextField3.setText(50000 - gestion.ultimoPlantel().getCotizacion() + "");
        } catch (Excepcion e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), e.getTitulo(), e.getTipoMensaje());
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int puesto = jComboBox1.getSelectedIndex();
	int club = jComboBox2.getSelectedIndex();
	club -= 1;

        int cot_min = jComboBox3.getSelectedIndex() == -1 ? 0 :
            Integer.parseInt(jComboBox3.getSelectedItem().toString().trim());
        int cot_max;
        if(jComboBox4.getSelectedIndex() <= 0)
            cot_max = PlantelFecha.cotizacion_max;
        else
            cot_max = Integer.parseInt(jComboBox4.getSelectedItem().toString().trim());

	jugadores = gestion.listarJugadores(puesto, club, cot_min, cot_max);
	int paginas = (int) Math.ceil((double) jugadores.length / jugXpag);

        if (paginas != 0) {
            mouseClicked(new MouseEvent(new JLabel("1"), 0, 0, 0, 0, 0, 0, false));
        } else {
            jugadores1ra.cambiarLista(new Jugador[jugXpag]);
            jTable3.updateUI();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
	try {
            String nombre = jTextField1.getText().trim();
            if(nombre.isEmpty())
                throw new DatosIncompletosException();
                        
            if (gestion.guardarEquipo(nombre)) {
                dispose();
                throw new CreacionEquipoExitosaException();
            }
        } catch(Excepcion e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), e.getTitulo(), e.getTipoMensaje());
        }

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
	if (jugSeleccionado < 11) {
	    if (jTable1.getSelectedRow() == jugSeleccionado) {
		jTable1.clearSelection();
		jugSeleccionado = -1;
	    } else
		jugSeleccionado = jTable1.getSelectedRow();
	} else {
	    jugSeleccionado = jTable1.getSelectedRow();
	    jTable2.clearSelection();
	}
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
	if (jugSeleccionado >= 11) {
	    if (jTable2.getSelectedRow() == jugSeleccionado - 11) {
		jTable2.clearSelection();
		jugSeleccionado = -1;
	    } else
		jugSeleccionado = jTable2.getSelectedRow() + 11;
	} else {
	    jugSeleccionado = jTable2.getSelectedRow() + 11;
	    jTable1.clearSelection();
	}
    }//GEN-LAST:event_jTable2MouseClicked




    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    public void mouseClicked(MouseEvent e) {
	JLabel comp = (JLabel) e.getComponent();
        int pagSeleccionada = Integer.parseInt(comp.getText().trim());

	int paginas = (int) Math.ceil((double) jugadores.length / jugXpag);

	int pagMostradas = paginas < pagVisibles ? paginas : pagVisibles;

	if (indices != null)
	    for(int i = 0; i < indices.length; i++) jPanel1.remove(indices[i]);

	indices = new JLabel[pagMostradas];

	if (paginas < 10 || pagSeleccionada <= 5) {
	    for (int i = 0; i < pagMostradas; i++) {
		indices[i] = new javax.swing.JLabel();
		indices[i].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		if (i == pagSeleccionada - 1)
		    indices[i].setFont(new java.awt.Font("Tahoma", 1, 16));
		else
		    indices[i].setFont(new java.awt.Font("Tahoma", 1, 14));
		indices[i].setForeground(new java.awt.Color(0, 102, 255));
		indices[i].setAlignmentY(JLabel.CENTER_ALIGNMENT);
		String tmp = " " + (i + 1) + (i + 1 > 9 ? "" : " ");
		indices[i].setText(tmp);
		indices[i].addMouseListener(this);
		jPanel1.add(indices[i]);
	    }
	} else if (pagSeleccionada > 5 && pagSeleccionada < paginas - 4) {
	    int j = 0;
	    for (int i = pagSeleccionada - 5; i < pagSeleccionada + 5; i++) {
		indices[j] = new javax.swing.JLabel();
		indices[j].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		if (i == pagSeleccionada - 1)
		    indices[j].setFont(new java.awt.Font("Tahoma", 1, 16));
		else
		    indices[j].setFont(new java.awt.Font("Tahoma", 1, 14));
		indices[j].setForeground(new java.awt.Color(0, 102, 255));
		indices[j].setAlignmentY(JLabel.CENTER_ALIGNMENT);
		String tmp = " " + (i + 1) + (i + 1 > 9 ? "" : " ");
		indices[j].setText(tmp);
		indices[j].addMouseListener(this);
		jPanel1.add(indices[j++]);
	    }
	} else {
	    int j = 0;
	   for (int i = paginas - pagVisibles; i < paginas; i++) {
		indices[j] = new javax.swing.JLabel();
		indices[j].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		if (i == pagSeleccionada - 1)
		    indices[j].setFont(new java.awt.Font("Tahoma", 1, 16));
		else
		    indices[j].setFont(new java.awt.Font("Tahoma", 1, 14));
		indices[j].setForeground(new java.awt.Color(0, 102, 255));
		indices[j].setAlignmentY(JLabel.CENTER_ALIGNMENT);
		String tmp = " " + (i + 1) + (i + 1 > 9 ? "" : " ");
		indices[j].setText(tmp);
		indices[j].addMouseListener(this);
		jPanel1.add(indices[j++]);
	    }
	}

        Jugador lista[] = new Jugador[jugXpag];

        int j = 0;
        for (int i = jugXpag * (pagSeleccionada - 1); i < jugXpag * pagSeleccionada && i < jugadores.length; i++)
            lista[j++] = jugadores[i];


        if (jugadores1ra != null)
            jugadores1ra.cambiarLista(lista);
        else
            jugadores1ra = new JugadorModel(lista);
        jPanel1.updateUI();
	jTable3.updateUI();
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}
